package workexp.alg.question;

import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class CheckInclusion {
    public boolean checkInclusion(String s1, String s2) {
        int left=0;
        int right=0+s1.length()-1;

        int i=0;
        boolean find=false;

        Map<Character,Integer> charset = new HashMap<>(s1.length());
        while(right<s2.length()){
            for (int j = left; j <= right; j++) {
                char tmpchar = s2.charAt(j);
                if(charset.containsKey(tmpchar)){
                    int count = charset.get(tmpchar);
                    charset.put(tmpchar,count+1);
                }else{
                    charset.put(tmpchar,1);
                }
            }
            for (int k = 0; k <s1.length() ; k++) {
                if(!charset.containsKey(s1.charAt(k))){
                    break;
                }else{
                    int count = charset.get(s1.charAt(k));
                    count--;
                    if(count<0){
                        break;
                    }
                    charset.put(s1.charAt(k),count);

                }
                if(k==s1.length()-1){
                    find=true;
                }
            }
            if(find){
                break;
            }else{
                charset.clear();
            }
            left++;
            right++;
        }
        return find;

    }

    @Test
    public void test(){
        boolean flag = checkInclusion("hello","ooolleoooleh");
        System.out.println(flag);
    }
}
